import 'package:get/get.dart';

class DateUtil {
  static const defaultFormat = 'yyyy-MM-dd HH:mm';
  static const defaultEnFormat = 'dd/MM/yyyy';
  static const defaultCnFormat = 'yyyy/MM/dd';
  static const defaultEnFormatLine = 'dd-MM-yyyy';
  static const defaultCnFormatLine = 'yyyy-MM-dd';
  static const defaultEnTimeFormat = 'HH:mm';

  /// 根据语言环境设置格式为 yyyy/MM/dd HH:mm 或者 dd/MM/yyyy HH:mm
  static String defaultAutoLocalFormat = Get.deviceLocale?.languageCode == 'zh'
      ? '$defaultCnFormat $defaultEnTimeFormat'
      : '$defaultEnFormat $defaultEnTimeFormat';

  /// 根据语言环境设置格式为 yyyy-MM-dd HH:mm 或者 dd-MM-yyyy HH:mm
  static String defaultAutoLocalFormatLine =
      Get.deviceLocale?.languageCode == 'zh'
          ? '$defaultCnFormatLine $defaultEnTimeFormat'
          : '$defaultEnFormatLine $defaultEnTimeFormat';

  /// 根据语言环境设置格式为 yyyy/MM/dd 或者 dd/MM/yyyy
  static String defaultAutoLocalFormatNoHour =
      Get.deviceLocale?.languageCode == 'zh'
          ? defaultCnFormat
          : defaultEnFormat;

  /// 根据语言环境设置格式为 yyyy-MM-dd 或者 dd-MM-yyyy
  static String defaultAutoLocalFormatLineNoHour =
      Get.deviceLocale?.languageCode == 'zh'
          ? defaultCnFormatLine
          : defaultEnFormatLine;

  /// get Now Date Milliseconds.
  static int getNowDateMs() {
    return DateTime.now().microsecondsSinceEpoch;
  }

  /// get Now Date Seconds.
  static int getNowDateS() {
    return DateTime.now().millisecondsSinceEpoch ~/ 1000;
  }

  static int getUTCDateS() {
    return DateTime.now().millisecondsSinceEpoch - 28800 * 1000;
  }

  static int toSecondsTime(DateTime dateTime) {
    return dateTime.millisecondsSinceEpoch ~/ 1000;
  }

  static DateTime addDaysToDate(DateTime date, int days) {
    return DateTime(date.year, date.month, date.day + days, date.hour,
        date.minute, date.second, date.millisecond, date.microsecond);
  }

  /// 格式化时间
  static String formatTime(int secondsTime, {String format = defaultFormat}) {
    if (secondsTime < 1) {
      return '---';
    }
    return formatDate(DateTime.fromMillisecondsSinceEpoch(secondsTime * 1000),
        format: format);
  }

  /// format date by DateTime.
  /// format 转换格式(已提供常用格式 DateFormats，可以自定义格式：'yyyy/MM/dd HH:mm:ss')
  /// 格式要求
  /// year -> yyyy/yy   month -> MM/M    day -> dd/d
  /// hour -> HH/H      minute -> mm/m   second -> ss/s
  static String formatDate(DateTime dateTime, {String format = defaultFormat}) {
    if (format.contains('yy')) {
      final year = dateTime.year.toString();
      if (format.contains('yyyy')) {
        format = format.replaceAll('yyyy', year);
      } else {
        format = format.replaceAll(
            'yy', year.substring(year.length - 2, year.length));
      }
    }

    format = _comFormat(dateTime.month, format, 'M', 'MM');
    format = _comFormat(dateTime.day, format, 'd', 'dd');
    format = _comFormat(dateTime.hour, format, 'H', 'HH');
    format = _comFormat(dateTime.minute, format, 'm', 'mm');
    format = _comFormat(dateTime.second, format, 's', 'ss');
    format = _comFormat(dateTime.millisecond, format, 'S', 'SSS');

    return format;
  }

  /// com format.
  static String _comFormat(
      int value, String format, String single, String full) {
    if (format.contains(single)) {
      if (format.contains(full)) {
        format =
            format.replaceAll(full, value < 10 ? '0$value' : value.toString());
      } else {
        format = format.replaceAll(single, value.toString());
      }
    }
    return format;
  }

  static String getWeekday(int secondsTime) {
    return 'weekday_${DateTime.fromMillisecondsSinceEpoch(secondsTime * 1000).weekday}'
        .tr;
  }
}
